//
//  MoreLabelView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/19.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol MoreLabelViewDelegate {
    @objc optional func selectLabel(index:IndexPath) //选择标签
    @objc optional func backAction() //返回
}

class MoreLabelView: UIView {
    
    weak var labelDelegate : MoreLabelViewDelegate?
    
    fileprivate let cellId = "moreLabelCell"
    
    var dataArray = [String]() {
        didSet{
            self.collectionView.reloadData()
        }
    }
    
    var collectionView : UICollectionView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.mineColor()
        
        let headView = UIView()
        headView.backgroundColor = UIColor.white
        self.addSubview(headView)
        
        let titleLabel = UILabel()
        titleLabel.text = moreInformation
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        headView.addSubview(titleLabel)
        
        let backButton = UIButton()
        backButton.layer.contents = #imageLiteral(resourceName: "up_arrow").cgImage
        backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        headView.addSubview(backButton)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = RATIO_H(maxNum: 20)
        layout.itemSize = .init(width: ScreenW/7*2, height: RATIO_H(maxNum: 30))
        layout.sectionInset = .init(top: 0, left: RATIO(maxNum: 8), bottom: 0, right: RATIO(maxNum: 8))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        headView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        backButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.centerY.equalToSuperview()
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 30))
            
        }
    }
    
    //MARK: 返回
    @objc fileprivate func backAction() {
        
        self.labelDelegate?.backAction!()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MoreLabelView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        cell?.contentView.backgroundColor = UIColor.white
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.labelDelegate?.selectLabel!(index: indexPath)
    }
    
}
